package com.radon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableZuulProxy //开启网关Zuul
@EnableEurekaClient //开启Eureka连接
public class StuZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(StuZuulApplication.class, args);
    }

    //zuul配置能够使用config实现实时更新
    @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties() {
        return new ZuulProperties();
    }
    //添加文档来源
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {

        @Override
        public List<SwaggerResource> get() {
            List<SwaggerResource> resource = new ArrayList<>();
            resource.add(swaggerResource("stu-producer","/api-producer/v2/api-docs","1.0"));
            resource.add(swaggerResource("stu-consumption","/api-consumption/v2/api-docs","1.0"));
            return resource;
        }
        /**
         *
         * @param name 此参数可以随意定义，用于查询接口模块名称
         * @param location /api-producer/v2/api-docs 中api-producer取自于网关的配置，这是对所有生产者访问时的域名前缀
         * @param version  版本号
         * @return
         */
        private SwaggerResource swaggerResource(String name, String location,String version) {
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }

    }

}
